package com.bank.annotation;

import javax.annotation.Resource;



import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.validation.Errors;
import com.bank.domain.Transaction;


public class TransactionValidator {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;	
	
	
	protected static Logger logger = Logger.getLogger("validator");
	public TransactionValidator(){
		
	}	
	
	public void validate(Transaction target, Errors errors, Double Available, Integer ownerId, Integer accountOwner, Integer toOwner) {
		
		Transaction transaction = (Transaction) target;
		if(transaction.getAccountFrom().equals(transaction.getAccountTo())){
			errors.rejectValue("accountFrom", "accountCollision","You can't transfer funds to the same account");
		}
		if((Available - transaction.getTransactionAmount()) < 0){
			errors.rejectValue("transactionAmount", "limitedFunds","There are insufficient funds");
		}
		
		if(transaction.getTransactionAmount() < 0){
			errors.rejectValue("transactionAmount", "underZerro","You cant sent negative amount");
		}		
		
		if(ownerId != accountOwner){
			errors.rejectValue("accountFrom", "WrongAccount","It's not Your account You try pay from");
		}	
		
		if(accountOwner == null){
			errors.rejectValue("accountFrom", "FromAccountDontExists" , "From Account dont exists");
		}			
		if(toOwner == null){
			errors.rejectValue("accountTo", "ToAccountDontExists" , "To Account dont exists");
		}		
		
		
    }

}